package com.ushine.web.base.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yls
 * @Date 2022/4/11 13:44
 * @Description: 执行bat文件
 * @Version 1.0
 **/
public class ExecWindows {
    public static final String batPath = FileUtils.getTestResourcePath() + "\\考勤打卡\\updateTime.bat";

    /**
     * 功能描述:
     * 〈 运行bat文件 〉
     *
     * @param
     * @return : void
     * @throws Exception
     * @author : yls
     * @date : 2022/4/11 13:46
     */
    public static void runbat() {
        String cmd = "cmd /c start ";
        Process ps = null;
        try {
            ps = Runtime.getRuntime().exec(cmd + batPath);
            Thread.sleep(1000);
        } catch (IOException | InterruptedException ioe) {
            ioe.printStackTrace();
        } finally {
            if (ps != null) {
                ps.destroy();
            }
        }
    }

    /**
     * 功能描述:
     * 〈 修改时间 〉
     *
     * @param date 年月日
     * @param time 时分秒
     * @return : void
     * @throws Exception
     * @author : yls
     * @date : 2020/12/30 13:51
     */
    public static void writeUpdateTimeToBat(String date, String time) throws IOException {
        List<String> contents = new ArrayList();
        contents.add("@echo off");
        contents.add("::获取管理员权限");
        contents.add("%1 mshta vbscript:CreateObject(\"Shell.Application\").ShellExecute(\"cmd.exe\",\"/c %~s0 ::\",\"\",\"runas\",1)(window.close)&&exit");
        contents.add("date " + date);
        contents.add("time " + time);
        FileUtils.writeFile(batPath, contents);
    }

    public static void main(String[] args) throws IOException {
        writeUpdateTimeToBat("2021-09-01", "12:05:45");
        ExecWindows.runbat();
    }
}
